package 剑指offerII;

import java.util.ArrayList;
import java.util.List;

public class _81允许重复选择元素的组合 {
    List<List<Integer>> lists = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    int sum = 0;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        dfs(candidates,0,target);
        return lists;
    }
    public void dfs(int[] candidates,int start,int target){
        if(start == candidates.length){
            return ;
        }
        if(sum == target){
            lists.add(new ArrayList(list));
            return ;
        }

        dfs(candidates,start + 1,target);

        if(sum + candidates[start] <= target){
            list.add(candidates[start]);
            sum += candidates[start];
            dfs(candidates,start,target);
            list.remove(list.size() - 1);
            sum -= candidates[start];
        }
    }
}
